﻿//-----------------------------------------------------------------
//
// @(#)$Id: CustomLableIncompleteCodeDetail.aspx.cs,v 1.0 2013/12/13 16:58:11 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 16:58:11 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.CenterDB_BasTableAdapters;

public partial class UI_Bas_CustomLableIncompleteCodeDetail : System.Web.UI.Page
{   
      string act = "";
      protected void Page_Load(object sender, EventArgs e)
      {
          act = Request.QueryString["act"];
          if (!IsPostBack)
          {
              switch (act)
              {
                  case "I":
                      break;
                  case "e":
                      {
                          string Id = Request.QueryString["id"];
                          string Id2 = Request.QueryString["id2"];
                          if (Id.IndexOf(" ") > 0)
                          {
                              Id = Id.Replace(" ", "+");
                          }
                          if (Id2.IndexOf(" ") > 0)
                          {
                              Id2 = Id2.Replace(" ", "+");
                          }
                          txtProductId.Attributes["readonly"] = "readonly";
                          T_Product2TableAdapter adapter = new T_Product2TableAdapter();
                          DataTable dt = adapter.GetDataByProductId(Id, Id2);
                          DataRow r = dt.Rows[0];
                          if (dt.Rows.Count > 0)
                          {
                              foreach (Control ctr in form1.Controls)
                              {
                                  if (ctr is HtmlInputText)
                                  {
                                      HtmlInputText txtbox = (HtmlInputText)ctr;
                                      string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                      txtbox.Value = r[name].ToString();
                                  }
                                  else if (ctr is HtmlSelect)
                                  {
                                      HtmlSelect ddlbox = (HtmlSelect)ctr;
                                      string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                      ddlbox.Value = r[name].ToString();
                                  }
                                  else if (ctr is DropDownList)
                                  {
                                      DropDownList ddlbox = (DropDownList)ctr;
                                      string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                      ddlbox.Text = r[name].ToString();
                                  }
                              }
                          }
                      }
                      break;
              }
          }
      }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime defaultDateTime = new DateTime(1900, 1, 1);

        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();

        if (!string.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "I":
                    {
                        T_Product2TableAdapter adapter = new T_Product2TableAdapter();
                        T_ProductTableAdapter  productadapter=new T_ProductTableAdapter ();
                       
                        int p2 = int.Parse(productadapter.ExistsByProductId(txtProductId2.Value).ToString());
                        if (txtProductId.Value != txtProductId2.Value)
                        {
                            if ( p2 > 0 && txtQty.Value != "")
                            {
                                int falg = int.Parse(adapter.IsExists(txtProductId.Value, txtProductId2.Value).ToString());
                                if (falg > 0)
                                {
                                    Response.Write("<script>alert('此不完全代码已经存在...!');</script>");
                                }
                                else
                                {
                                    int val = adapter.Insert(
                                        txtProductId.Value,
                                        txtProductId2.Value,
                                        Convert.ToInt32(txtQty.Value),
                                        userId,
                                        System.DateTime.Now,
                                        "",//Auser
                                        defaultDateTime
                                        );
                                    if (val > 0)
                                    {
                                        Response.Write("<script>alert('添加成功!');window.location.href='CustomLableIncompleteCode.aspx'</script>");
                                    }
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('产品代码输入有误或未填数量，请重新输入！...');</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('产品代码不能重复，请重新输入！...');</script>");
                        }
                    }
                    break;
                case "e":
                    {
                        string Id = Request.QueryString["id"];
                        string Id2 = Request.QueryString["id2"];
                        if (Id.IndexOf(" ") > 0)
                        {
                            Id = Id.Replace(" ", "+");
                        }
                        if (Id2.IndexOf(" ") > 0)
                        {
                            Id2 = Id2.Replace(" ", "+");
                        }                       
                        T_Product2TableAdapter adapter = new T_Product2TableAdapter();
                        DataTable dt = adapter.GetDataByProductId(Id,Id2);
                        DataRow r = dt.Rows[0];
                        T_ProductTableAdapter productadapter = new T_ProductTableAdapter();
                    
                        int p2 = int.Parse(productadapter.ExistsByProductId(txtProductId2.Value).ToString());
                        if (txtProductId.Value != txtProductId2.Value)
                        {
                            if (p2 > 0 && txtQty.Value != "")
                            {
                                if (dt.Rows.Count > 0)
                                {
                                    foreach (Control ctr in form1.Controls)
                                    {
                                        if (ctr is HtmlInputText)
                                        {
                                            HtmlInputText txtbox = (HtmlInputText)ctr;
                                            string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                            r[name] = txtbox.Value;
                                        }
                                        else if (ctr is HtmlSelect)
                                        {
                                            HtmlSelect ddlbox = (HtmlSelect)ctr;
                                            string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                            r[name] = ddlbox.Value;
                                        }
                                        else if (ctr is DropDownList)
                                        {
                                            DropDownList ddlbox = (DropDownList)ctr;
                                            string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                            r[name] = ddlbox.SelectedIndex;
                                        }
                                    }
                                    r["Muser"] = userId;
                                    r["Mtime"] = System.DateTime.Now;
                                    r["Auser"] = "";
                                    r["Atime"] = System.DateTime.Now;
                                }
                                int val = adapter.Update(r);
                                if (val > 0)
                                {
                                    Response.Write("<script>alert('修改成功!');window.location.href='CustomLableIncompleteCode.aspx'</script>");
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('产品代码输入有误或未填数量，请重新输入！...');</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('产品代码不能重复，请重新输入！...');</script>");
                        }
                    }
                    break;
            }
        }
    }       
}